﻿Public Class frmRelanceFA
    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing

    Private Sub frmRelanceFA_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        CB_TYPE.SelectedIndex = 0
        CB_RELANCE.SelectedIndex = 0
        TE_ECHEANCE.Value = DateAdd("d", -15, Now)
    End Sub

    Private Sub TE_ECHEANCE_ValueChanged(sender As System.Object, e As System.EventArgs) Handles TE_ECHEANCE.ValueChanged
        InitListeDOSSIERS(0)
    End Sub

    Private Sub InitListeDOSSIERS(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' générer la Table des Relanceq
        szSQL = "CALL spINS_T_RELANCE('" & gUserCode & "','" & gSocieteCode & "','" & CB_TYPE.SelectedItem.ToString.Substring(0, 1) & "','" & _
            Format(TE_ECHEANCE.Value, "yyyy/MM/dd") & "')"
        ExecQuery(ctDSN_DEVFAC, szSQL)

        ' effacer la listview
        LV_DOSSIERS.Items.Clear()

        szSQL = "CALL spGET_T_RELANCE('" & gUserCode & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)

        Do While Not rs.EOF
            LV_DOSSIERS.Items.Add(Format(rs.Fields("no_fact").Value, "000000"))
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("date").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("nom").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("chantier").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("mt_facture").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("mt_aregler").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("niveau").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(nv(rs.Fields("daterel").Value))
            LV_DOSSIERS.Items(nb).SubItems.Add(nv(rs.Fields("daterel2").Value))
            LV_DOSSIERS.Items(nb).SubItems.Add(nv(rs.Fields("cd_cli").Value))
            If rs.Fields("niveau").Value > "1" Then
                LV_DOSSIERS.Items(nb).ForeColor = Color.Red
            End If
            ' Get the next record
            nb = nb + 1
            rs.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        LB_NbLignes.Text = CStr(nb) & " fiche(s)"
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_DOSSIERS)
        BO_IMPRIMER.Enabled = True

    End Sub

    Private Sub BO_IMPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_IMPRIMER.Click

        If LV_DOSSIERS.SelectedItems.Count = 0 Then
            Exit Sub
        End If
        'Mettre à jour la date de relance de la Facture
        szSQL = "spUPD_FACTURE_RELANCE('" & gSocieteCode & "'," & LV_DOSSIERS.SelectedItems(0).SubItems(0).Text & "," & _
                 CB_RELANCE.SelectedIndex + 1 & ")"
        ExecQuery(ctDSN_DEVFAC, szSQL)

        If CB_TYPE.SelectedItem.ToString.Substring(0, 1) <> "P" Then  'Si type de Client = Société
            Select Case gSocieteCode
                Case "PO"
                    frmViewReport.pNomEtat = "Edi_RelS-PO.rpt"
                Case "PL"
                    frmViewReport.pNomEtat = "Edi_RelS-PL.rpt"
                Case Else
                    frmViewReport.pNomEtat = "Edi_RelS.rpt"
            End Select
        Else  'Si type de Client = Particulier
            Select Case gSocieteCode
                Case "PO"
                    If CB_RELANCE.SelectedIndex = 0 Then
                        frmViewReport.pNomEtat = "Edi_RelP1-PO.rpt"
                    Else
                        frmViewReport.pNomEtat = "Edi_RelP2-PO.rpt"
                    End If
                Case "PL"
                    If CB_RELANCE.SelectedIndex = 0 Then
                        frmViewReport.pNomEtat = "Edi_RelP1-PL.rpt"
                    Else
                        frmViewReport.pNomEtat = "Edi_RelP2-PL.rpt"
                    End If
                Case Else
                    If CB_RELANCE.SelectedIndex = 0 Then
                        frmViewReport.pNomEtat = "Edi_RelP1.rpt"
                    Else
                        frmViewReport.pNomEtat = "Edi_RelP2.rpt"
                    End If
            End Select
        End If
        gCD_CLI = LV_DOSSIERS.SelectedItems(0).SubItems(9).Text
        frmViewReport.pParamRPT = ""
        frmViewReport.ShowDialog()

    End Sub

    Private Sub LV_DOSSIERS_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles LV_DOSSIERS.SelectedIndexChanged

        If LV_DOSSIERS.SelectedItems.Count > 0 Then
            TE_CHANTIER.Text = LV_DOSSIERS.SelectedItems(0).SubItems(3).Text
            BO_IMPRIMER.Enabled = True
        Else
            TE_CHANTIER.Text = ""
            BO_IMPRIMER.Enabled = False
        End If

    End Sub
End Class